package mysql

import "fmt"

type Single struct {
	Id int64	`json:"Id" xorm:"pk autoincr"`
	Song_id int `json:"song_id"`
	Song_name string `json:"song_name"`
	Singer string `json:"singer"`
	Album string `json:"album"`
	Duration int `json:"duration"`
	//Createtime int `json:"createtime"xorm:"created"`
	Sheet_id int `json:"sheet_id"`
	Singer_id int `json:"singer_id"`
}

func(sink MysqlSink) GetSingleList(page int,pagesize int ,keyword string) (rows []Single,err error) {
	fmt.Println(1)
	rows = make([]Single, 0)
	err = sink.database.Where("song_name like ?", "%"+keyword+"%").Limit(pagesize, page*pagesize).Find(&rows)
	if err !=nil{
		fmt.Println(err)
		return
	}
	return rows,nil
	//fmt.Println(users)
}
//判断是否存在
func (sink MysqlSink) GetSingle(song_id int)(has bool,err error)  {
	model := new(Single)
	has, err = sink.database.Where("song_id=?", song_id).Get(model)
	if err !=nil{
		fmt.Println(err)
		return
	}
	return has,nil
}
func (sink MysqlSink)GetSingles(singer_id int)(rows []Single,err error) {
	model := make([]Single, 0)
	err = sink.database.Where(" singer_id = ?", singer_id).Find(&model)
	if err !=nil{
		fmt.Println(err)
		return
	}
	return model,nil
}

//插入数据
func (sink MysqlSink) InsertSingle(data Single)(res int64,err error)  {
	affected, err :=sink.database.Insert(data)
	if err !=nil{
		fmt.Println(err)
		return
	}
	return affected,nil
}
